<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-form-item label="供应商名称" prop="supplier_name">
                <el-input v-model="form.supplier_name" placeholder="请输入供应商名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="对接人" prop="supplier_contacts">
                <el-input v-model="form.supplier_contacts" placeholder="请输入对接人" clearable></el-input>
            </el-form-item>
            <el-form-item label="联系方式" prop="supplier_phone">
                <el-input v-model="form.supplier_phone" placeholder="请输入联系方式" clearable></el-input>
            </el-form-item>
            <el-form-item label="地址" prop="supplier_address">
                <el-input v-model="form.supplier_address" placeholder="请输入地址" clearable type="textarea"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" prop="create_time">
                <el-date-picker v-model="form.create_time" type="date" style="width: 100%;" :value-format="'YYYY-MM-DD'" disabled></el-date-picker>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增供应商',
				edit: '编辑供应商',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
				supplier_name: "",
				supplier_contacts: "",
				supplier_phone: "",
				supplier_address: "",
                create_time: this.moment().format('YYYY-MM-DD'),
			},
			//验证规则
			rules: {
				enterprise: [{ required: true, message: '请输入名称' }],
			},
            depts: [],
			deptsProps: {
				value: "id",
				checkStrictly: true
			},
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.apply.apply.edit.post(this.form);
					} else {
						res = await this.$API.apply.apply.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
            this.form.id = data.id
            this.form.supplier_name = data.supplier_name
            this.form.supplier_contacts = data.supplier_contacts
            this.form.supplier_phone = data.supplier_phone
            this.form.supplier_address = data.supplier_address
            this.form.create_time = data.create_time
		},
	}
}
</script>

<style></style>
